package cn.larry.tools.files;

import cn.larry.tools.string.StringTool;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Larry
 * 2021/10/29 08:38
 *
 * @Version 1.1
 */
public class FilesTool {

    /**
     * 追加内容至文件尾部
     *
     * @param fileName
     * @param content
     */
    @Deprecated
    public static void addfielcontent(String fileName, String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content);
        } catch (IOException e) {
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 一行一行的读取文件内容
     *
     * @param path
     * @return
     */
    public static String readfile(String path) {
        File file = new File(path);
        StringBuilder result = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (Exception e) {
            }
        }
        return result.toString();
    }

    /**
     * 获取文件夹下面所有文件全路径
     *
     * @param path
     * @return
     */
    public static List<String> getFilePathNames(String path) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        List<String> filePathNames = new LinkedList<>();

//        System.out.println("该目录下对象个数：" + tempList.length);
        for (int i = 0; i < tempList.length; i++) {
            if (!tempList[i].isDirectory()) {
//                System.out.println(tempList[i].getParentFile() + "/" + tempList[i].getName());
                filePathNames.add(tempList[i].getParentFile() + "/" + tempList[i].getName());
            }
        }
        return filePathNames;
    }


    /**
     * 创建文件
     *
     * @param fileName 文件全路径
     * @throws Exception
     */
    public static void mkdirFile(String fileName) throws Exception {
        if (StringTool.isNotBlank(fileName)) {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
    }

    /**
     * 创建文件
     *
     * @param path      文件夹路径
     * @param childFile 文件夹下的文件名称
     * @throws Exception
     */
    public static void mkdirDirAndFile(String path, String childFile) throws Exception {
        if (StringTool.isNotBlank(path)) {
            File dir = new File(path);
            if (!dir.exists() && !dir.isDirectory()) {
                dir.mkdir();
            }
        }
        if (StringTool.isNotBlank(childFile)) {
            if (!path.endsWith("/"))
                path += "/";
            mkdirFile(path + childFile);
        }
    }

    /**
     * 追加内容至文件尾部行
     *
     * @param fileName 文件全路径
     * @param content  追加内容
     * @throws Exception
     */
    public static void appendToFileTail(String fileName, String content) throws Exception {
        FileWriter writer = null;
        try {
            //打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content + "\n");
            writer.close();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 写入文件
     *
     * @param ins
     * @param file
     */
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
